package com.hyfrogx.modules.app.controller.sign;

import com.hyfrogx.common.utils.R;
import com.hyfrogx.modules.app.annotation.Login;
import com.hyfrogx.modules.sign.entity.IntegralConfig;
import com.hyfrogx.modules.app.service.IntegralConfigService;
import com.hyfrogx.modules.app.utils.FrogConstant;
import com.hyfrogx.modules.manage.controller.BaseController;
import com.hyfrogx.modules.sign.entity.SignGrade;
import com.hyfrogx.modules.sign.service.SignGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liubin
 * @date 2019/11/28 17:38
 */

@RestController
@RequestMapping(value = "api/sign/")
@Slf4j
@Api(value = "ApiSignController",tags = {"客户端-签到模块"})
public class ApiSignController extends BaseController {

  @Autowired
  private SignGradeService signService;
  @Autowired
  private IntegralConfigService configService;

  private ReentrantLock lock = new ReentrantLock();

  @PostMapping(value = "createSignRecord")
  @Login
  @ApiOperation("签到")
  public R createSignRecord(@ApiIgnore @RequestAttribute("userId") Long userId) throws Exception {
    lock.lock();
    try {
      int grade=signService.sign(userId);
      if(grade>0){
        return R.ok().put("data",grade);
      }
      if(grade==0){
        return R.error("您已签到");
      }
      if(grade==-1){
        return R.error("签到异常");
      }
      return R.ok().put("data",grade);
    } catch (Exception e) {
      return R.error(e.getMessage());
    } finally {
      lock.unlock();
    }
  }

  @PostMapping(value = "isSign")
  @Login
  @ApiOperation("查询是否签到")
  public R isSign(@ApiIgnore @RequestAttribute("userId") Long userId) throws Exception {
    int result =signService.isSign(userId);
    if(result>0){
      return R.ok().put("data",1);
    }
    return R.ok().put("data",0);
  }

  @PostMapping(value = "getSignRecord")
  @Login
  @ApiOperation("查询签到记录")
  public R getSignRecord(@ApiIgnore @RequestAttribute("userId") Long userId) throws Exception {
    SignGrade grade =signService.getSignRecord1(userId);
    grade.setSignRule(FrogConstant.SignRule.SIGNRULE);
    List<IntegralConfig> config = configService.getSignIconList();
    HashMap<String, Object> result=new HashMap<>();
    result.put("signGrade", grade);
    result.put("integralConfig", config);
    return R.ok().put("data",result);
  }

  /**
   * 获取我的当天签到积分
   */
  @PostMapping(value = "getIntegerScore")
  @Login
  @ApiOperation("查询当天签到积分")
  public R getIntegerScore(@ApiIgnore @RequestAttribute("userId") Long userId) throws Exception {
    SignGrade grade =signService.getSignRecord1(userId);
    return R.ok().put("data",grade);
  }

  //获取我的总积分
  /*@RequestMapping(value = "getScore")
  @Login
  public R getScore(@ApiIgnore @RequestAttribute("userId") Long userId) throws Exception {
    if (userId == null) {
      return R.error("用户ID不能为空");
    }
    try {
      int score = signService.getScore(userId.intValue());
      return R.ok().put("data",score);
    } catch (ServiceException e) {
      return R.error(e.getMessage());
    }
  }*/

}
